﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region Module Attributes
	#FullScreen: False
	#IncludeTitle: True
#End Region

'Activity module
Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.
	Dim scvList As ScrollView
	
  Dim pnlHeader As Panel
  Dim pnlTable As Panel
  Dim NumberOfColumns, RowHeight, RowHeight_1, ColumnWidth, ColumnWidth_1 As Int
  Dim HeaderColor, LineColor, CellColor, FontColor, HeaderFontColor As Int
  Dim ColLineWidth, RowLineWidth As Int
  Dim FontSize As Float
  
  Dim SelectedRow As Int
  Dim SelectedRowColor As Int : SelectedRowColor=Colors.LightGray
	
  'Table settings
  HeaderColor = Colors.Gray
  NumberOfColumns = 2 'will be overwritten when loading from CSV file.
  ColLineWidth = 1dip
  RowLineWidth = 1dip
  RowHeight_1 = 30dip
  RowHeight=RowHeight_1+RowLineWidth
  LineColor = Colors.Black
  CellColor = Colors.White
  FontColor = Colors.Black
  HeaderFontColor = Colors.White
  FontSize = 16

End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormZDZCXM")
	Activity.Title = "支出项目"	
	
	pnlTable = scvList.Panel
  pnlTable.Color = LineColor
  
  SelectedRow = -1
	
	InitLayOut
	InitData
	
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub InitLayOut
	
End Sub

Sub InitData
	Dim sSQL As String
	
	sSQL = "SELECT * FROM ZD_ZCXM ORDER BY BM"
	
	LoadData(sSQL)
	
End Sub

Sub LoadData(ASQL As String)
	Dim mapResult,mapRow As Map
	Dim lsRecords As List
	Dim result,code,key As String
	Dim DataSetList As Cursor
	Dim h(4) As String
	Dim w(4) As Int
	
	
	h(0) = "编码"
	h(1) = "名称"
	h(2) = "单位"
	h(3) = "单价"
	
	w(0) = 15%X
	w(1) = 45%X
	w(2) = 20%X	
	w(3) = 20%X
	
	ClearAll
		
	NumberOfColumns = h.Length
'  ColumnWidth = scvList.Width / 4
'  ColumnWidth_1 = ColumnWidth-ColLineWidth
  SetHeader(h,w)	
		
	DataSetList = PublicVar.GV_SQLTool.ExecQuery(ASQL)
	Dim data(NumberOfColumns) As String
	For i = 0 To DataSetList.RowCount - 1
		DataSetList.Position = i
		Dim row() As String
		data(0) = DataSetList.GetString("BM") 
		data(1) = DataSetList.GetString("MC") 
		data(2) = DataSetList.GetString("DW")
		data(3) = DataSetList.GetString("DJ")		
		row =data
    AddRow(row,w)

	Next
	DataSetList.Close

End Sub

Sub ClearAll
    For i = pnlTable.NumberOfViews -1 To 0 Step -1
        pnlTable.RemoveViewAt(i)
    Next
    pnlTable.Height = 0
    SelectedRow = -1
End Sub

Sub SetHeader(Values() As String,Width() As Int)
    Dim iWidth As Int		
		iWidth = 1
		
		If pnlHeader.IsInitialized Then Return 'should only be called once
    pnlHeader.Initialize("")
		
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
  
				l.Initialize("header")
        l.Text = Values(i)
        l.Gravity = Gravity.CENTER
        l.TextSize = FontSize
        l.Color = HeaderColor
        l.TextColor = HeaderFontColor
        l.Tag = i
        pnlHeader.AddView(l, iWidth, 0, Width(i) - ColLineWidth, RowHeight_1)
				iWidth = iWidth + Width(i)
    Next
    Activity.AddView(pnlHeader, scvList.Left, scvList.Top - RowHeight, scvList.Width, RowHeight)
End Sub

Sub AddRow(Values() As String,Width() As Int)
    If Values.Length <> NumberOfColumns Then
        Log("Wrong number of values.")
        Return
    End If
    Dim lastRow As Int
    Dim iWidth As Int		
		iWidth = 1
		
    lastRow = NumberOfRows
    For i = 0 To NumberOfColumns - 1
        Dim l As Label
				
        l.Initialize("cell")
        l.Text = Values(i)
				
				Select i 
					Case 0,1,2
						l.Gravity = Gravity.LEFT + Gravity.CENTER_VERTICAL
					Case Else
						l.Gravity = Gravity.RIGHT + Gravity.CENTER_VERTICAL
				End Select
				
        l.TextSize = FontSize
        l.TextColor = FontColor
        l.Color=Colors.White
        Dim rc As RowCol
        rc.Initialize
        rc.Col = i
        rc.Row = lastRow
        l.Tag = rc
        pnlTable.AddView(l, iWidth, RowHeight * lastRow, Width(i) - ColLineWidth, RowHeight_1)
				iWidth = iWidth + Width(i)
    Next
    pnlTable.Height = NumberOfRows * RowHeight
End Sub

Sub NumberOfRows As Int
    Return pnlTable.NumberOfViews / NumberOfColumns
End Sub
Sub Cell_Click
    Dim rc As RowCol
    Dim l As Label
    Dim l0 As Label
    l = Sender
    rc = l.Tag
    SelectRow(rc.Row)
    l0=GetView(rc.Row,0)
    Activity.Title = "Cell clicked: (" & rc.Row & ", " & rc.Col & l0.Text &")"     
     
End Sub

Sub Header_Click
    Dim l As Label
    Dim col As Int
    l = Sender
    col = l.Tag
    Activity.Title = "Header clicked: " & col
End Sub

Sub SelectRow(Row As Int)
    'remove the color of previously selected row
    If SelectedRow > -1 Then
        For col = 0 To NumberOfColumns - 1
            GetView(SelectedRow, col).Color = CellColor
        Next
    End If
    SelectedRow = Row
    For col = 0 To NumberOfColumns - 1
        GetView(Row, col).Color = SelectedRowColor
    Next
End Sub

'Returns the label in the specific cell
Sub GetView(Row As Int, Col As Int) As Label
    Dim l As Label
    l = pnlTable.GetView(Row * NumberOfColumns + Col)
    Return l
End Sub


